import axios from "@/assets/request/request";
import { isNull } from "@/utils/common";
import { Ref, ref } from "vue"
import { LoginInfo } from '@/types/user'

// 文件down预览
export default {
  sendCode: (data: string) => {
    return axios({
      url: `/common/sendCode/${data}`,
      method: "get"
    });
  },
  getVideo: (fileName: string) => {
    return isNull(fileName) ? "" : `/api/common/download/video/${fileName}`;
  },
  getCover: (fileName: string) => {
    return isNull(fileName) ? "" : `/api/common/download/cover/${fileName}`;
  },
  getHeadshot: (fileName: string) => {
    fileName = isNull(fileName) ? "default.png" : fileName;
    return `/api/common/download/headshot/${fileName}`;
  },
  /**
   * 判断用户是否还有未上传的视频
   */
  haveNoUploadVideo: () => {
    return axios({
      url: "/common/tmpUpload",
      method: "get"
    });
  },
  /**
   * 上传视频
   * @param formData FormData文件格式对象
   * @param uploadProgress 上传进度
   * @returns {*}
   */
  uploadVideo: (formData: FormData, uploadProgress: Ref<number>) => {
    return axios({
      headers: {
        'Content-Type': 'multipart/form-data;charset=UTF-8'
      },
      url: "/common/upload/video",
      method: "post",
      data: formData,
      onUploadProgress: (progressEvent: any) => {
        // ts-ignore
        let rate = ((progressEvent.loaded / progressEvent.total * 100) | 0);
        //实时获取上传进度
        if (rate === 100) {
          uploadProgress.value = 99;
        } else {
          uploadProgress.value = rate;
        }
      }
    });
  },

  uploadCover: (formData: FormData) => {
    return axios({
      headers: {
        'Content-Type': 'multipart/form-data;charset=UTF-8'
      },
      url: "/common/upload/cover",
      method: "post",
      data: formData
    });
  },

  uploadHeadshot: (formData: FormData) => {
    return axios({
      headers: {
        'Content-Type': 'multipart/form-data;charset=UTF-8'
      },
      url: "/common/upload/headshot",
      method: "post",
      data: formData
    });
  }
};